package chances.mock.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import chances.epg.commons.utils.RequestUtils;
import chances.epg.user.controller.UserAbstractController;
import chances.mock.entity.MsgEntity;
import chances.mock.entity.RequestEntity;
import chances.mock.model.BindResponse;
import chances.mock.model.MsgResponse;

@RestController
@RequestMapping(value = "/gateway")
public class GatewayBindController extends UserAbstractController {

	@RequestMapping(value = "/bind/mgr")
	public String bind(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {

		String sid = RequestUtils.getParameter(request, "sid", "");
		String cmd = RequestUtils.getParameter(request, "cmd", "");
		String appid = RequestUtils.getParameter(request, "appid", "");
		String iptvacct = RequestUtils.getParameter(request, "iptvacct", "");
		String stbtype = RequestUtils.getParameter(request, "stbtype", "");
		String epgversion = RequestUtils.getParameter(request, "epgversion", "");
		String tradegroup = RequestUtils.getParameter(request, "tradegroup", "");
		String areagroup = RequestUtils.getParameter(request, "areagroup", "");
		String lifetime = RequestUtils.getParameter(request, "lifetime", "");
		String timestamp = RequestUtils.getParameter(request, "timestamp", "");
		String sig = RequestUtils.getParameter(request, "sig", "");
		RequestEntity entity = new RequestEntity(sid, cmd, appid, iptvacct, stbtype, epgversion, tradegroup, areagroup,
				lifetime, timestamp, sig);
		RequestEntity.listRequest.add(entity);
		BindResponse res = new BindResponse();
		return res.toString();
	}

	@RequestMapping(value = "/push/mgr")
	public String push(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {

		String cmd = RequestUtils.getParameter(request, "cmd", "");
		String sid = RequestUtils.getParameter(request, "sid", "");
		String timestamp = RequestUtils.getParameter(request, "timestamp", "");
		String sig = RequestUtils.getParameter(request, "sig", "");
		String appid = RequestUtils.getParameter(request, "appid", "");
		String starttime = RequestUtils.getParameter(request, "starttime", "");
		int msgtype = RequestUtils.getParameter(request, "msgtype", 0);
		String expire = RequestUtils.getParameter(request, "expire", "");
		String epg = RequestUtils.getParameter(request, "epg", "");
		String receivers = RequestUtils.getParameter(request, "receivers", "");
		String content = RequestUtils.getParameter(request, "content", "");

		long starttimeVal = System.currentTimeMillis();
		if (StringUtils.isNotEmpty(starttime)) {
			starttimeVal = Long.parseLong(starttime);
		}

		long expireVal = System.currentTimeMillis();
		if (StringUtils.isNotEmpty(expire)) {
			expireVal = Long.parseLong(expire);
		}

		long timestampVal = System.currentTimeMillis();
		if (StringUtils.isNotEmpty(timestamp)) {
			timestampVal = Long.parseLong(timestamp);
		}

		MsgEntity msgEntity = new MsgEntity(sid, cmd, appid, starttimeVal, msgtype, expireVal, timestampVal, epg,
				receivers, content, sig);
		MsgEntity.listRequest.add(msgEntity);
		MsgResponse res = new MsgResponse();
		res.setMsgId(19481);
		res.setResCode("0");
		res.setResDesc("成功");
		return res.toString();
	}
	
}
